package com.e3.web.widget.client;

import java.util.List;

import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;

public class MyRadioGroup<X> extends RadioGroup implements MyField<X> {
	
	private boolean isHide = false;

	public MyRadioGroup() {
		super();
	}

	public MyRadioGroup(String name) {
		super(name);
	}

	public void addRadio(MyRadio<X> radio) {
		this.add(radio);
	}

	public void addRadio(String label, X value, boolean isSelected) {
		MyRadio<X> radio = new MyRadio<X>();
		radio.setBoxLabel(label);
		radio.setValue(isSelected);
		radio.setFieldValue(value);
		addRadio(radio);
	}

	@SuppressWarnings("unchecked")
	public X getFieldValue() {
		List<Field<?>> fields = getAll();
		for (Field<?> field : fields) {
			MyRadio<X> radio = (MyRadio<X>) field;
			if (radio.getValue()) {
				return radio.getFieldValue();
			}
		}
		return (X)this.getValue();
	}

	@SuppressWarnings("unchecked")
	public void setFieldValue(Object value) {
		List<Field<?>> fields = getAll();
		for (Field<?> field : fields) {
			MyRadio<X> radio = (MyRadio<X>) field;
			if (radio.getFieldValue() == null) {
				continue;
			}
			if (radio.getFieldValue().equals(value)) {
				radio.setValue(true);
				break;
			}
		}
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		List<Field<?>> fields = getAll();
		for (Field<?> field : fields) {
			field.setReadOnly(readOnly);
			field.setEnabled(!readOnly);
		}
	}
	@Override
	public void hide() {
		isHide = true;
		super.hide();
	}

	public boolean isHide() {
		return isHide;
	}
}
